package com.zjut.an.acquisitionhistory.action;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.zjut.an.acquisitionhistory.dao.AnAcquisitionHistory;
import com.zjut.an.acquisitionhistory.manager.AnAcquisitionHistoryManager;
import com.zjut.an.util.module.PageModule;
import com.zjut.an.util.tools.JSONUtilClass;

public class AnAcquisitionHistoryAction extends ActionSupport implements
ServletResponseAware, ServletRequestAware, ModelDriven<AnAcquisitionHistory>{
	private HttpServletRequest request;
	private HttpServletResponse response;
	private HttpSession session;
	
	private AnAcquisitionHistory obj;
	private Map<String , Object> dataMap;
	private int page = 1, limit = 10;
	private String order;
	private String sort;
	private String search;
	private String rowstr;
	
	@Resource(name = "AnAcquisitionHistoryManager")
	private AnAcquisitionHistoryManager manager;
	
	public String anAcquisitionHistoryList() throws UnsupportedEncodingException {
		try {
			if (session.getAttribute("admin")!=null) {//只有管理员才允许执行该操作，以免信息泄露
				request.setCharacterEncoding("UTF-8");
				dataMap = new HashMap<String, Object>();
				//int pageNumber = offset/limit + 1;
				
				PageModule module = manager.getDataByInstance(page, limit, sort, order, obj);
				System.out.println("@@@@@@@@@@ "+module.getObjects().size());
				dataMap.put("rows", module.getObjects());
				dataMap.put("total", module.getTotal()); 
				obj = null; 
			}			
			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}
	}
	
	@SuppressWarnings("unchecked")
	public String anAcquisitionHistoryDeletes() {
		if (session.getAttribute("admin")!=null) {//只有管理员才允许执行该操作，以免信息泄露
			dataMap = new HashMap<String, Object>();
			String objumniJSON = rowstr;
			if ("报名序号编号".equals(request.getParameter("channelUrl"))) {
				manager.deleteDatas(manager.getDataByChannelUrl("报名序号编号"));
			} else if ("抽签编号".equals(request.getParameter("channelUrl"))) {
				manager.deleteDatas(manager.getDataByChannelUrl("抽签编号"));
			} else if ("现场准考证编号".equals(request.getParameter("channelUrl"))) {
				manager.deleteDatas(manager.getDataByChannelUrl("现场准考证编号"));
			}
			dataMap.put("flag", "success");
		}		
		return SUCCESS;
	}
	
	/*
	 * public String AnAcquisitionHistoryAdd() { if
	 * (session.getAttribute("admin")!=null) {//只有管理员才允许执行该操作，以免信息泄露 String
	 * objumniJSON = rowstr; obj =
	 * (AnAcquisitionHistory)JSONUtilClass.getDTO(objumniJSON,
	 * AnAcquisitionHistory.class); manager.addData(obj); } return SUCCESS; }
	 */
	
	@Override
	public AnAcquisitionHistory getModel() {
		if (null == obj) {
			obj = new AnAcquisitionHistory();
		}
		return obj;
	}

	@Override
	public void setServletRequest(HttpServletRequest arg0) {
		request = arg0;
		session = request.getSession();
	}

	@Override
	public void setServletResponse(HttpServletResponse arg0) {
		response = arg0;
	}

	public HttpServletRequest getRequest() {
		return request;
	}

	public void setRequest(HttpServletRequest request) {
		this.request = request;
	}

	public HttpServletResponse getResponse() {
		return response;
	}

	public void setResponse(HttpServletResponse response) {
		this.response = response;
	}

	public HttpSession getSession() {
		return session;
	}

	public void setSession(HttpSession session) {
		this.session = session;
	}

	public AnAcquisitionHistory getAl() {
		return obj;
	}

	public void setAl(AnAcquisitionHistory obj) {
		this.obj = obj;
	}

	public Map<String, Object> getDataMap() {
		return dataMap;
	}

	public void setDataMap(Map<String, Object> dataMap) {
		this.dataMap = dataMap;
	}

	public String getRowstr() {
		return rowstr;
	}

	public void setRowstr(String rowstr) {
		this.rowstr = rowstr;
	}

	public AnAcquisitionHistoryManager getManager() {
		return manager;
	}

	public void setManager(AnAcquisitionHistoryManager manager) {
		this.manager = manager;
	}

	public int getLimit() {
		return limit;
	}

	public void setLimit(int limit) {
		this.limit = limit;
	}

	public String getOrder() {
		return order;
	}

	public void setOrder(String order) {
		this.order = order;
	}

	public String getSort() {
		return sort;
	}

	public void setSort(String sort) {
		this.sort = sort;
	}

	public String getSearch() {
		return search;
	}

	public void setSearch(String search) {
		this.search = search;
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}
}
